using BanZuiCloudControlAgent.Utils.Common;
using Newtonsoft.Json;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// 配置消息负载模型
    /// </summary>
    public class ConfigPayloadModel
    {
        /// <summary>
        /// 状态上报间隔（秒）
        /// </summary>
        [JsonProperty("statusReportInterval")]
        public int StatusReportInterval { get; set; }

        /// <summary>
        /// 空闲状态上报间隔（秒）
        /// </summary>
        [JsonProperty("idleStatusReportInterval")]
        public int IdleStatusReportInterval { get; set; }

        /// <summary>
        /// 离线阈值（秒）
        /// </summary>
        [JsonProperty("offlineThreshold")]
        public int OfflineThreshold { get; set; }

        /// <summary>
        /// 任务执行超时时间（秒）
        /// </summary>
        [JsonProperty("taskExecutionTimeout")]
        public int TaskExecutionTimeout { get; set; }

        /// <summary>
        /// 最大并发任务数
        /// </summary>
        [JsonProperty("maxConcurrentTasks")]
        public int MaxConcurrentTasks { get; set; }

        /// <summary>
        /// 是否启用自动重连
        /// </summary>
        [JsonProperty("enableAutoReconnect")]
        public bool EnableAutoReconnect { get; set; }

        /// <summary>
        /// 最大重连尝试次数
        /// </summary>
        [JsonProperty("maxReconnectAttempts")]
        public int MaxReconnectAttempts { get; set; }

        /// <summary>
        /// 是否启用日志上传
        /// </summary>
        [JsonProperty("enableLogUpload")]
        public bool EnableLogUpload { get; set; }

        /// <summary>
        /// 日志上传间隔（秒）
        /// </summary>
        [JsonProperty("logUploadInterval")]
        public int LogUploadInterval { get; set; }

        /// <summary>
        /// 是否启用自动更新
        /// </summary>
        [JsonProperty("enableAutoUpdate")]
        public bool EnableAutoUpdate { get; set; }

        /// <summary>
        /// 自动更新检查间隔（秒）
        /// </summary>
        [JsonProperty("autoUpdateCheckInterval")]
        public int AutoUpdateCheckInterval { get; set; }
    }

    /// <summary>
    /// 配置消息模型
    /// </summary>
    public class ConfigModel : BaseMessageModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigModel() : base()
        {
            MessageType = MessageTypeConstants.CONFIG;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="payload">配置负载</param>
        public ConfigModel(string deviceCode, ConfigPayloadModel payload)
            : base(MessageTypeConstants.CONFIG, deviceCode, payload)
        {
        }

        /// <summary>
        /// 获取配置负载
        /// </summary>
        /// <returns>配置负载</returns>
        public ConfigPayloadModel GetPayload()
        {
            if (Payload == null)
                return null;
                
            if (Payload is ConfigPayloadModel payloadModel)
                return payloadModel;
                
            // 尝试将Payload转换为ConfigPayloadModel
            try
            {
                // 如果Payload是JObject或其他JSON对象，尝试转换
                return JsonHelper.Convert<object, ConfigPayloadModel>(Payload);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 创建默认配置
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <returns>配置消息模型</returns>
        public static ConfigModel CreateDefaultConfig(string deviceCode)
        {
            var payload = new ConfigPayloadModel
            {
                StatusReportInterval = 30,
                IdleStatusReportInterval = 180,
                OfflineThreshold = 300,
                TaskExecutionTimeout = 3600,
                MaxConcurrentTasks = 3,
                EnableAutoReconnect = true,
                MaxReconnectAttempts = 20,
                EnableLogUpload = true,
                LogUploadInterval = 3600,
                EnableAutoUpdate = true,
                AutoUpdateCheckInterval = 86400
            };

            return new ConfigModel(deviceCode, payload);
        }
    }
} 